package com.esri.android.map;

import android.content.Context;
import com.esri.core.internal.RuntimeHelper;
import com.esri.core.internal.tasks.ags.d;
import com.esri.core.internal.tasks.e;
import com.esri.core.internal.util.g;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.AttachmentInfo;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/* loaded from: classes.dex */
public class AttachmentManager {
    private static final String a = "AttachmentManager";
    private static final String b = "ArcGISCache";
    private static final int c = 24;
    private String d;
    private UserCredentials e;
    private File f;
    private boolean g;
    private Context h;

    /* loaded from: classes.dex */
    public interface AttachmentDownloadListener {
        void onAttachmentDownloadFailed(Exception exc);

        void onAttachmentDownloaded(File file);

        void onAttachmentDownloading();
    }

    /* loaded from: classes.dex */
    public interface AttachmentRetrievalListener {
        void onAttachmentRetrieved(List<AttachmentInfo> list);

        void onAttachmentRetrieving();
    }

    public AttachmentManager(Context context, String str, UserCredentials userCredentials, File file) {
        this.d = str;
        this.e = userCredentials;
        this.f = file;
        this.h = context;
        if (file == null) {
            this.g = false;
            File a2 = RuntimeHelper.a(context);
            if (a2 != null) {
                this.f = new File(a2, b);
            }
        } else {
            this.g = true;
        }
        File file2 = this.f;
        if (file2 != null) {
            file2.mkdirs();
        }
    }

    public Future<List<AttachmentInfo>> retrieveAttachmentInfos(final long j, final AttachmentRetrievalListener attachmentRetrievalListener) {
        if (attachmentRetrievalListener != null) {
            attachmentRetrievalListener.onAttachmentRetrieving();
        }
        return e.a().submit(new Callable<List<AttachmentInfo>>() { // from class: com.esri.android.map.AttachmentManager.1
            @Override // java.util.concurrent.Callable
            /* renamed from: a */
            public List<AttachmentInfo> call() throws Exception {
                return AttachmentManager.this.a(j, attachmentRetrievalListener);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public class a extends Exception {
        private static final long serialVersionUID = 1;

        a(String str) {
            super(str);
        }
    }

    public Future<File> downloadAttachment(final long j, final int i, final String str, final AttachmentDownloadListener attachmentDownloadListener) {
        if (attachmentDownloadListener != null) {
            attachmentDownloadListener.onAttachmentDownloading();
        }
        return e.a().submit(new Callable<File>() { // from class: com.esri.android.map.AttachmentManager.2
            /* JADX WARN: Removed duplicated region for block: B:103:0x0145 A[ADDED_TO_REGION] */
            /* JADX WARN: Removed duplicated region for block: B:109:0x0158  */
            /* JADX WARN: Removed duplicated region for block: B:121:0x0129 A[EXC_TOP_SPLITTER, SYNTHETIC] */
            /* JADX WARN: Removed duplicated region for block: B:124:0x009b A[EXC_TOP_SPLITTER, SYNTHETIC] */
            /* JADX WARN: Removed duplicated region for block: B:126:0x00d9 A[EXC_TOP_SPLITTER, SYNTHETIC] */
            /* JADX WARN: Removed duplicated region for block: B:51:0x00ca  */
            /* JADX WARN: Removed duplicated region for block: B:85:0x011f A[Catch: all -> 0x0125, TRY_LEAVE, TryCatch #4 {all -> 0x0125, blocks: (B:83:0x011b, B:85:0x011f), top: B:119:0x011b }] */
            /* JADX WARN: Removed duplicated region for block: B:96:0x0130 A[ADDED_TO_REGION] */
            @Override // java.util.concurrent.Callable
            /* renamed from: a */
            /*
                Code decompiled incorrectly, please refer to instructions dump.
                To view partially-correct code enable 'Show inconsistent code' option in preferences
            */
            public java.io.File call() throws java.lang.Exception {
                /*
                    Method dump skipped, instructions count: 350
                    To view this dump change 'Code comments level' option to 'DEBUG'
                */
                throw new UnsupportedOperationException("Method not decompiled: com.esri.android.map.AttachmentManager.AnonymousClass2.call():java.io.File");
            }
        });
    }

    public boolean deleteAttachment(String str) {
        if (str == null) {
            return true;
        }
        File attachmentFile = getAttachmentFile(str);
        if (attachmentFile != null) {
            return attachmentFile.delete();
        }
        return false;
    }

    public File getAttachmentFile(String str) {
        if (str == null) {
            return null;
        }
        File file = new File(this.f, str);
        if (!this.g) {
            file.deleteOnExit();
        }
        return file;
    }

    public File getAttachmentDownloadDir() {
        return this.f;
    }

    public void setAttachmentDownloadDir(File file) {
        this.f = file;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public List<AttachmentInfo> a(long j, AttachmentRetrievalListener attachmentRetrievalListener) throws Exception {
        ArrayList arrayList = new ArrayList(0);
        try {
            try {
                if (g.a(this.d)) {
                    throw new IllegalArgumentException("Invalid URL.");
                }
                com.esri.core.internal.tasks.ags.c cVar = new com.esri.core.internal.tasks.ags.c();
                cVar.a(j);
                if (Thread.currentThread().isInterrupted()) {
                    throw new InterruptedException();
                }
                AttachmentInfo[] execute = new d(cVar, this.d, this.e).execute();
                if (execute != null) {
                    arrayList.addAll(Arrays.asList(execute));
                }
                return arrayList;
            } catch (Exception e) {
                throw e;
            }
        } finally {
            if (attachmentRetrievalListener != null) {
                attachmentRetrievalListener.onAttachmentRetrieved(arrayList);
            }
        }
    }
}
